using System;
using System.Net;

namespace Voodoo.Analytics
{
	internal class VoodooWebProxy : IWebProxy
	{
		private IWebProxy _wrappedProxy;

		private ICredentials _credentials;

		private string _proxyServer;

		public ICredentials Credentials
		{
			get
			{
				if (_wrappedProxy != null)
				{
					return _wrappedProxy.Credentials;
				}
				return _credentials;
			}
			set
			{
				if (_wrappedProxy != null)
				{
					_wrappedProxy.Credentials = value;
				}
				else
				{
					_credentials = value;
				}
			}
		}

		private void Init()
		{
			_wrappedProxy = null;
			_credentials = CredentialCache.DefaultCredentials;
		}

		public VoodooWebProxy(string proxyServer)
		{
			_proxyServer = proxyServer;
			Init();
		}

		public VoodooWebProxy(IWebProxy theWrappedProxy)
		{
			Init();
			_wrappedProxy = theWrappedProxy;
		}

		public Uri GetProxy(Uri destination)
		{
			if (_wrappedProxy != null)
			{
				return _wrappedProxy.GetProxy(destination);
			}
			return new Uri("http://" + _proxyServer + ":8888");
		}

		public bool IsBypassed(Uri host)
		{
			if (_wrappedProxy != null)
			{
				return _wrappedProxy.IsBypassed(host);
			}
			return false;
		}
	}
}
